package com.lixinlei.security.api.service;

import java.io.IOException;
import java.util.List;

import com.lixinlei.security.api.dao.UserRepository;
import com.lixinlei.security.api.entity.User;
import com.lixinlei.security.api.vo.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lambdaworks.crypto.SCryptUtil;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserInfo create(UserInfo info) throws IOException {
        User user = new User();
        BeanUtils.copyProperties(info, user);
        // 数据加密，同样的明文，加密的结果是不一样的
        user.setPassword(SCryptUtil.scrypt(user.getPassword(), 32768, 8, 1));
        userRepository.save(user);
        info.setId(user.getId());
        return info;
    }

    @Override
    public UserInfo update(UserInfo user) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void delete(Long id) {
        // TODO Auto-generated method stub

    }

    @Override
    public UserInfo get(Long id) throws IOException {
        return userRepository.findById(id).get().buildInfo();
    }

    @Override
    public List<UserInfo> query(String name) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public UserInfo login(UserInfo info) {
        UserInfo result = null;
        User user = userRepository.findByUsername(info.getUsername());
        if(user != null && SCryptUtil.check(info.getPassword(), user.getPassword())) {
            result = user.buildInfo();
        }
        return result;
    }

}
